package com.win.oauth.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.win.oauth.constant.OAuthConst;
import com.win.oauth.dto.FunPermissionDTO;
import com.win.oauth.entity.Fun;
import com.win.oauth.entity.Module;
import com.win.oauth.entity.Permission;
import com.win.oauth.mapper.FunMapper;
import com.win.oauth.service.IFunService;
import com.win.oauth.service.IModuleService;
import com.win.oauth.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author youlian.fei
 * @since 2020-03-14
 */
@Service
public class FunServiceImpl extends ServiceImpl<FunMapper, Fun> implements IFunService {

    @Autowired
    IPermissionService permissionService;

    @Autowired
    IModuleService moduleService;

    @Override
    public List<FunPermissionDTO> selectByModule(Long moduleId) {
        return baseMapper.selectByModule(moduleId);
    }

    @Override
    public void saveCascade(Fun fun, Permission permission) {
        this.saveOrUpdate(fun);

        Long moduleId = fun.getModuleId();
        Module module = moduleService.lambdaQuery().eq(Module::getId, moduleId).one();
        fun.setNode(module.getNode() + OAuthConst.TREE_NODE_DELIMITER + fun.getId());
        this.updateById(fun);

        permission.setModuleFunId(fun.getId());
        permissionService.saveOrUpdate(permission);
    }

    @Override
    public void delCascade(Fun fun, Permission permission) {
        permissionService.removeById(permission.getId());
        this.removeById(fun.getId());
    }
}
